package com.sdp.irrigation.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.cache.IrrigationCleanCache;
import com.sdp.irrigation.db.irrigation_pump_well.service.IIrrigationPumpWellService;
import com.sdp.irrigation.db.irrigation_pump_well.service.impl.IrrigationPumpWellServiceImpl;
import com.sdp.irrigation.entity.IrrigationPumpWell;
import com.sdp.irrigation.view.IrrigationPumpWellView;

/**
 * <p>
 * 机井台账信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/IrrigationPumpWellController")
public class IrrigationPumpWellController {

	
	@Autowired
	private IIrrigationPumpWellService iIrrigationPumpWellService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;	
	
	@Autowired
	private IrrigationCleanCache irrigationCleanCache;
	
	@Autowired
	private IrrigationPumpWellServiceImpl irrigationPumpWellServiceImpl;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationPumpWellService, IrrigationPumpWell.class, dataCenter));
	}
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<IrrigationPumpWellView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(irrigationPumpWellServiceImpl, IrrigationPumpWellView.class, dataCenter, queryWrapper));
	}
	
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationPumpWell pumpWel) {
		if(pumpWel==null) {
			return Result.error("机井信息为空");
		}
		if(StringUtils.isEmpty(pumpWel.getEqNo()) || StringUtils.isEmpty(pumpWel.getPwNo()) || StringUtils.isEmpty(pumpWel.getPwName())) {
			return Result.error("机井名称、机井编号、设备编号不能为空");
		}
		if(StringUtils.isEmpty(pumpWel.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationPumpWellService, IrrigationPumpWell.class, new String[] {"pwNo","pwName"}, "keyId", pumpWel, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("机井名称、机井编号不能重复");
			}
			
			iIrrigationPumpWellService.addNewPumpWell(pumpWel);
			
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationPumpWellService, IrrigationPumpWell.class, new String[] {"pwNo","pwName"}, "keyId", null, pumpWel, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("机井名称、机井编号不能重复");
			}
			
			IrrigationPumpWell oldPumpWell = iIrrigationPumpWellService.getById(pumpWel.getKeyId());
			if(oldPumpWell==null) {
				return Result.error("此机井信息已不存在，请先确认");
			}

			iIrrigationPumpWellService.updatePumpWell(oldPumpWell, pumpWel);
			
		}
		irrigationCleanCache.cleanPumpWell();
		
		//根据设备编号获取设备封面图
//		IrrigationEq eq = iIrrigationEqService.queryEqByEqNo(pumpWel.getEqNo());
//		pumpWel.setEqCoverimg(eq.getEqCoverimg());
//		iIrrigationPumpWellService.updateById(pumpWel);
		
		return Result.ok(pumpWel);
	}
	
}

